<?php

namespace RedMVC;

/**
 * View class
 *  
 * @category    RedMVC
 * @author      Jan Fischer, bitWorking <info@bitworking.de>
 */
class View{

	protected $_vars;
	protected $_file;

    public function __construct($file = ''){
		$this->init($file);
	}
	
	public function init($file = ''){
        $this->_file = $file;        
        $this->_vars = array();
	}
    
    public function assign($name, $value){
        $this->_vars[$name] = $value;
    }
    
    public function __set($name, $value){
        $this->_vars[$name] = $value;
    }
    
    public function __get($name){
        if(isset($this->_vars[$name])){
            return $this->_vars[$name];
        }
        return null;
    }
    
    public function __isset($name){
        return isset($this->_vars[$name]);
    }
    
    public function __unset($name){
        unset($this->_vars[$name]);
    }
    
    public function mergeArray(array $vars){
        $this->_vars = array_merge($this->_vars, $vars);        
    }
    
    public function assignArray(array $vars){
        $this->_vars = $vars;
    }
 
    public function render(){        
        return $this->_parsePHP($this->_file);        
    }
    
    protected function _parsePHP($file){
        extract($this->_vars);        
        ob_start();        
        include($file);
        return ob_get_clean();
    }
    
    public function __call($method, $args){
        $viewHelper = Registry::get('RedMVC_ViewHelper');
        return call_user_func_array(array($viewHelper, $method), $args);
    }
	
}